package com.doctor.windflower_doctor.myuser.tools;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.GZIPInputStream;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.HttpVersion;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import com.lidroid.xutils.http.client.multipart.HttpMultipartMode;
import com.lidroid.xutils.http.client.multipart.MultipartEntity;
import com.lidroid.xutils.http.client.multipart.content.FileBody;
import com.lidroid.xutils.http.client.multipart.content.StringBody;

public class HttpclientHelper {
	public static String clientGet(String url) {
		HttpClient client = new DefaultHttpClient();
		
		HttpGet get = new HttpGet(url);

		HttpResponse f;
		try {
			f = client.execute(get);

			if (f.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = f.getEntity();

				return EntityUtils.toString(entity, "utf-8");
			}
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;

	}

	public static String anonymityPostRegister(String eq_no, String role,
			String appsecret, String pre_period, String baby_date, String url) {
		DefaultHttpClient httpClient = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost(url);
		List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
		list.add(new BasicNameValuePair("eq_no", eq_no));
		list.add(new BasicNameValuePair("role", role));
		list.add(new BasicNameValuePair("pre_period", pre_period));
		list.add(new BasicNameValuePair("appsecret", appsecret));
		list.add(new BasicNameValuePair("baby_date", baby_date));
		try {
			httpPost.setEntity(new UrlEncodedFormEntity(list, "utf-8"));
			HttpResponse response = httpClient.execute(httpPost);
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();

				return EntityUtils.toString(entity, "utf-8");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return url;

	}

	public static String postSendAuthCode(String telephone, String appsecret,
			String url) {
		DefaultHttpClient httpClient = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost(url);
		List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
		list.add(new BasicNameValuePair("telephone", telephone));
		list.add(new BasicNameValuePair("appsecret", appsecret));

		try {
			httpPost.setEntity(new UrlEncodedFormEntity(list, "utf-8"));
			HttpResponse response = httpClient.execute(httpPost);

			if (response.getStatusLine().getStatusCode() == 200) {

				HttpEntity entity = response.getEntity();
				System.out.println();
				return EntityUtils.toString(entity, "utf-8");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return url;

	}

	public static String move(String[] userId, String groupId, String url) {
		DefaultHttpClient httpClient = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost(url);
		List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
		for (int i = 0; i < userId.length; i++) {
			list.add(new BasicNameValuePair("userIds[]", userId[i]));
		}

		list.add(new BasicNameValuePair("groupId", groupId));

		try {
			httpPost.setEntity(new UrlEncodedFormEntity(list, "utf-8"));
			HttpResponse response = httpClient.execute(httpPost);

			if (response.getStatusLine().getStatusCode() == 200) {

				HttpEntity entity = response.getEntity();
				System.out.println();
				return EntityUtils.toString(entity, "utf-8");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return url;

	}

	public static String postRemoveGroupName(String groupName, String url) {
		DefaultHttpClient httpClient = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost(url);
		List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
		list.add(new BasicNameValuePair("groupId", groupName));

		try {
			httpPost.setEntity(new UrlEncodedFormEntity(list, "utf-8"));
			HttpResponse response = httpClient.execute(httpPost);

			if (response.getStatusLine().getStatusCode() == 200) {

				HttpEntity entity = response.getEntity();
				System.out.println();
				return EntityUtils.toString(entity, "utf-8");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return url;

	}

	public static String postAddGroupName(String groupName, String url) {
		DefaultHttpClient httpClient = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost(url);
		List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
		list.add(new BasicNameValuePair("groupName", groupName));

		try {
			httpPost.setEntity(new UrlEncodedFormEntity(list, "utf-8"));
			HttpResponse response = httpClient.execute(httpPost);

			if (response.getStatusLine().getStatusCode() == 200) {

				HttpEntity entity = response.getEntity();
				System.out.println();
				return EntityUtils.toString(entity, "utf-8");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return url;

	}

	public static String postReplaceGroupName(String groupName, String id,
			String url) {
		DefaultHttpClient httpClient = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost(url);
		List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
		list.add(new BasicNameValuePair("groupName", groupName));
		list.add(new BasicNameValuePair("groupId", id));

		try {
			httpPost.setEntity(new UrlEncodedFormEntity(list, "utf-8"));
			HttpResponse response = httpClient.execute(httpPost);

			if (response.getStatusLine().getStatusCode() == 200) {

				HttpEntity entity = response.getEntity();
				System.out.println();
				return EntityUtils.toString(entity, "utf-8");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return url;

	}

	public static String smsSendAuthCode(String telephone, String code,
			String appsecret, String url) {
		DefaultHttpClient httpClient = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost(url);
		List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
		list.add(new BasicNameValuePair("telephone", telephone));
		list.add(new BasicNameValuePair("code", code));
		list.add(new BasicNameValuePair("appsecret", appsecret));

		try {
			httpPost.setEntity(new UrlEncodedFormEntity(list, "utf-8"));
			HttpResponse response = httpClient.execute(httpPost);
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();
				System.out.println();
				return EntityUtils.toString(entity, "utf-8");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return url;

	}

	public static String Register(String username, String password,
			String authcode, String url) {
		DefaultHttpClient httpClient = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost(url);
		List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
		list.add(new BasicNameValuePair("username", username));
		list.add(new BasicNameValuePair("password", password));
		list.add(new BasicNameValuePair("authcode", authcode));
		try {
			httpPost.setEntity(new UrlEncodedFormEntity(list, "utf-8"));
			HttpResponse response = httpClient.execute(httpPost);
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();
				return EntityUtils.toString(entity, "utf-8");
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;

	}

	public static String authCode(String telephone,String appsecret,

	String url) {
		DefaultHttpClient httpClient = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost(url);

		List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
		list.add(new BasicNameValuePair("telephone", telephone));
		list.add(new BasicNameValuePair("appsecret", appsecret));

		try {
			httpPost.setEntity(new UrlEncodedFormEntity(list, "utf-8"));
			HttpResponse response = httpClient.execute(httpPost);
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();
				return EntityUtils.toString(entity, "utf-8");
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;

	}
	public static String VerifyAuthCode(String telephone,String appsecret,String code,

			String url) {
				DefaultHttpClient httpClient = new DefaultHttpClient();
				HttpPost httpPost = new HttpPost(url);

				List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
				list.add(new BasicNameValuePair("telephone", telephone));
				list.add(new BasicNameValuePair("appsecret", appsecret));
				list.add(new BasicNameValuePair("code", code));
				try {
					httpPost.setEntity(new UrlEncodedFormEntity(list, "utf-8"));
					HttpResponse response = httpClient.execute(httpPost);
					if (response.getStatusLine().getStatusCode() == 200) {
						HttpEntity entity = response.getEntity();
						return EntityUtils.toString(entity, "utf-8");
					}

				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				return "youwenti";

			}
	public static String login(String telephone, String password, String email,
			String appsecret, String url) {
		DefaultHttpClient httpClient = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost(url);

		List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
		list.add(new BasicNameValuePair("telephone", telephone));
		list.add(new BasicNameValuePair("password", password));
		list.add(new BasicNameValuePair("email", email));
		list.add(new BasicNameValuePair("appsecret", appsecret));

		try {
			httpPost.setEntity(new UrlEncodedFormEntity(list, "utf-8"));
			HttpResponse response = httpClient.execute(httpPost);
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();
				return EntityUtils.toString(entity, "utf-8");
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return url;

	}

	public static String smsPhone(String telephone, String appsecret, String url) {

		DefaultHttpClient httpClient = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost(url);

		List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
		list.add(new BasicNameValuePair("telephone", telephone));

		list.add(new BasicNameValuePair("appsecret", appsecret));

		try {
			httpPost.setEntity(new UrlEncodedFormEntity(list, "utf-8"));
			HttpResponse response = httpClient.execute(httpPost);
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();

				return EntityUtils.toString(entity, "utf-8");
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return url;

	}

	public static String sendEmailAuth(String email, String appsecret,
			String url) {
		DefaultHttpClient httpClient = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost(url);

		List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
		list.add(new BasicNameValuePair("email", email));

		list.add(new BasicNameValuePair("appsecret", appsecret));

		try {
			httpPost.setEntity(new UrlEncodedFormEntity(list, "utf-8"));
			HttpResponse response = httpClient.execute(httpPost);
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();
				return EntityUtils.toString(entity, "utf-8");
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return url;

	}

	public static String smsEmail(String email, String appsecret, String url) {

		DefaultHttpClient httpClient = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost(url);

		List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
		list.add(new BasicNameValuePair("email", email));

		list.add(new BasicNameValuePair("appsecret", appsecret));

		try {
			httpPost.setEntity(new UrlEncodedFormEntity(list, "utf-8"));
			HttpResponse response = httpClient.execute(httpPost);
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();

				return EntityUtils.toString(entity, "utf-8");
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return url;

	}

	// public static String modificationPhone_Passpord(String telephone, String
	// code, String newpwd, String appsecret, String url) {
	// DefaultHttpClient httpClient = new DefaultHttpClient();
	// HttpPost httpPost = new HttpPost(url);
	//
	// List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
	// list.add(new BasicNameValuePair("telephone", telephone));
	// list.add(new BasicNameValuePair("newpwd", newpwd));
	// list.add(new BasicNameValuePair("appsecret", APPSECRET_USER));
	// list.add(new BasicNameValuePair("code", code));
	//
	// try {
	// httpPost.setEntity(new UrlEncodedFormEntity(list, "utf-8"));
	// HttpResponse response = httpClient.execute(httpPost);
	// if (response.getStatusLine().getStatusCode() == 200) {
	// HttpEntity entity = response.getEntity();
	// return EntityUtils.toString(entity, "utf-8");
	// }
	//
	// } catch (Exception e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	// return url;
	//
	// }

	public static String modificationEmail_Password(String email, String code,
			String newpwd, String appsecret, String url) {
		DefaultHttpClient httpClient = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost(url);

		List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
		list.add(new BasicNameValuePair("email", email));
		list.add(new BasicNameValuePair("newpwd", newpwd));
		list.add(new BasicNameValuePair("appsecret", appsecret));
		list.add(new BasicNameValuePair("code", code));

		try {
			httpPost.setEntity(new UrlEncodedFormEntity(list, "utf-8"));
			HttpResponse response = httpClient.execute(httpPost);
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();
				return EntityUtils.toString(entity, "utf-8");
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return url;

	}

	// public static String task_Record(String tableName, Notes note, String
	// url, String action) {
	//
	// DefaultHttpClient httpClient = new DefaultHttpClient();
	// HttpPost httpPost = new HttpPost(url);
	// httpPost.setHeader(HTTP.CONTENT_TYPE, "application/json;charset=utf-8");
	// JSONObject json = new JSONObject();
	// JSONObject json2 = new JSONObject();
	// try {
	// json2.put("uuid", note.getUuid());
	// json2.put("userid", note.getUserid());
	// json2.put("days", note.getDays());
	// if (!StringUtil.isEmpty(note.getWeight()))
	// json2.put("weight", note.getWeight());
	// if (!StringUtil.isEmpty(note.getTemperature()))
	// json2.put("temperature", note.getTemperature());
	// if (!StringUtil.isEmpty(note.getBellyLength()))
	// json2.put("bellyLength", note.getBellyLength());
	// if (!StringUtil.isEmpty(note.getBloodPressure()))
	// json2.put("bloodPressure", note.getBloodPressure());
	// if(note.getPurge()!=null)
	// json2.put("purge", note.getPurge());
	//
	// if (!StringUtil.isEmpty(note.getSleepTime()))
	// json2.put("sleepTime", note.getSleepTime());
	// if (!StringUtil.isEmpty(note.getExerciseTime()))
	// json2.put("exerciseTime", note.getExerciseTime());
	// if (!StringUtil.isEmpty(note.getFetalMovement()))
	// json2.put("fetalMovement", note.getFetalMovement());
	// if (!StringUtil.isEmpty(note.getSymptom()))
	// json2.put("symptom", note.getSymptom());
	//
	// json2.put("createdAt", note.getCreatedAt());
	//
	// } catch (JSONException e2) {
	// // TODO Auto-generated catch block
	// e2.printStackTrace();
	// }
	//
	// try {
	//
	// json.put("data", json2);
	// json.put("tableName", tableName);
	// json.put("action", action);
	//
	// } catch (JSONException e1) {
	// // TODO Auto-generated catch block
	// e1.printStackTrace();
	// }
	//
	// StringEntity se;
	// try {
	//
	// se = new StringEntity(json.toString(), "utf-8");
	//
	// httpPost.setEntity(se);
	// } catch (UnsupportedEncodingException e1) {
	// // TODO Auto-generated catch block
	// e1.printStackTrace();
	// }
	//
	// try {
	//
	// HttpResponse response = httpClient.execute(httpPost);
	//
	// if (response.getStatusLine().getStatusCode() == 200) {
	// HttpEntity entity = response.getEntity();
	// return EntityUtils.toString(entity, "utf-8");
	// }
	//
	// } catch (Exception e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	// return url;
	//
	// }

	// public static String task_Inspection(String tableName, Inspection
	// inspection, String url, String action) {
	//
	// DefaultHttpClient httpClient = new DefaultHttpClient();
	// HttpPost httpPost = new HttpPost(url);
	// httpPost.setHeader(HTTP.CONTENT_TYPE, "application/json;charset=utf-8");
	// JSONObject json = new JSONObject();
	// JSONObject json2 = new JSONObject();
	// try {
	// if (!StringUtil.isEmpty(inspection.getUuid()))
	// json2.put("uuid", inspection.getUuid());
	// if (!StringUtil.isEmpty(inspection.getUserid()))
	// json2.put("userid", inspection.getUserid());
	// json2.put("days", inspection.getDays());
	// if (!StringUtil.isEmpty(inspection.getTitle()))
	// json2.put("title", inspection.getTitle());
	// if (!StringUtil.isEmpty(inspection.getPhotos()))
	// json2.put("photos", inspection.getPhotos());
	//
	// json2.put("createdAt", inspection.getCreatedAt());
	// json2.put("status", inspection.getStatus());
	//
	// } catch (JSONException e2) {
	// // TODO Auto-generated catch block
	// e2.printStackTrace();
	// }
	//
	// try {
	//
	// json.put("data", json2);
	// json.put("tableName", tableName);
	// json.put("action", action);
	//
	// } catch (JSONException e1) {
	// // TODO Auto-generated catch block
	// e1.printStackTrace();
	// }
	//
	// StringEntity se;
	// try {
	//
	// se = new StringEntity(json.toString(), "utf-8");
	//
	// httpPost.setEntity(se);
	// } catch (UnsupportedEncodingException e1) {
	// // TODO Auto-generated catch block
	// e1.printStackTrace();
	// }
	//
	// try {
	//
	// HttpResponse response = httpClient.execute(httpPost);
	// if (response.getStatusLine().getStatusCode() == 200) {
	// HttpEntity entity = response.getEntity();
	// return EntityUtils.toString(entity, "utf-8");
	// }
	//
	// } catch (Exception e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	// return url;
	//
	// }

	// public static String task_Todo(String tableName, Todo todo, String url,
	// String action) {
	//
	// DefaultHttpClient httpClient = new DefaultHttpClient();
	// HttpPost httpPost = new HttpPost(url);
	// httpPost.setHeader(HTTP.CONTENT_TYPE, "application/json;charset=utf-8");
	// JSONObject json = new JSONObject();
	// JSONObject json2 = new JSONObject();
	// try {
	// json2.put("uuid", todo.getUuid());
	// json2.put("userid", todo.getUserid());
	// json2.put("days", todo.getDays());
	//
	// json2.put("content", todo.getContent());
	//
	// json2.put("complete", todo.getComplete());
	// json2.put("startAt", todo.getStartAt());
	// json2.put("valid", todo.getValid());
	// json2.put("createdAt", todo.getCreatedAt());
	// json2.put("type", todo.getType());
	// json2.put("status", todo.getStatus());
	//
	// } catch (JSONException e2) {
	// // TODO Auto-generated catch block
	// e2.printStackTrace();
	// }
	//
	// try {
	//
	// json.put("data", json2);
	// json.put("tableName", tableName);
	// json.put("action", action);
	//
	// } catch (JSONException e1) {
	// // TODO Auto-generated catch block
	// e1.printStackTrace();
	// }
	//
	// StringEntity se;
	// try {
	//
	// se = new StringEntity(json.toString(), "utf-8");
	//
	// httpPost.setEntity(se);
	// } catch (UnsupportedEncodingException e1) {
	// // TODO Auto-generated catch block
	// e1.printStackTrace();
	// }
	//
	// try {
	//
	// HttpResponse response = httpClient.execute(httpPost);
	// if (response.getStatusLine().getStatusCode() == 200) {
	// HttpEntity entity = response.getEntity();
	// return EntityUtils.toString(entity, "utf-8");
	// }
	//
	// } catch (Exception e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	// return url;
	//
	// }

	// public static String task_Note(String tableName, Essay essay, String url,
	// String action) {
	//
	// DefaultHttpClient httpClient = new DefaultHttpClient();
	// HttpPost httpPost = new HttpPost(url);
	// httpPost.setHeader(HTTP.CONTENT_TYPE, "application/json;charset=utf-8");
	// JSONObject json = new JSONObject();
	// JSONObject json2 = new JSONObject();
	// try {
	// json2.put("uuid", essay.getUuid());
	// json2.put("userid", essay.getUserid());
	// json2.put("days", essay.getDays());
	// json2.put("content", essay.getContent());
	// json2.put("location", essay.getLocation());
	// json2.put("address", essay.getAddress());
	// json2.put("photos", essay.getPhotos());
	// json2.put("createdAt", essay.getCreatedAt());
	//
	// } catch (JSONException e2) {
	// // TODO Auto-generated catch block
	// e2.printStackTrace();
	// }
	//
	// try {
	//
	// json.put("data", json2);
	// json.put("tableName", tableName);
	// json.put("action", action);
	//
	// } catch (JSONException e1) {
	// // TODO Auto-generated catch block
	// e1.printStackTrace();
	// }
	//
	// StringEntity se;
	// try {
	//
	// se = new StringEntity(json.toString(), "utf-8");
	//
	// httpPost.setEntity(se);
	// } catch (UnsupportedEncodingException e1) {
	// // TODO Auto-generated catch block
	// e1.printStackTrace();
	// }
	//
	// try {
	//
	// HttpResponse response = httpClient.execute(httpPost);
	// if (response.getStatusLine().getStatusCode() == 200) {
	// HttpEntity entity = response.getEntity();
	// return EntityUtils.toString(entity, "utf-8");
	// }
	//
	// } catch (Exception e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	// return url;
	//
	// }

	public static String getAddress(String url) {
		DefaultHttpClient client = new DefaultHttpClient();
		HttpGet httpGet = new HttpGet(url);
		try {
			HttpResponse response = client.execute(httpGet);
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();

				return EntityUtils.toString(entity, "utf-8");
			}
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;

	}

	public String clientPost(String url, List<BasicNameValuePair> list,
			Context context) {
		DefaultHttpClient client = new DefaultHttpClient();
		HttpClientParams.setCookiePolicy(client.getParams(),
				CookiePolicy.BROWSER_COMPATIBILITY);
		HttpPost post = new HttpPost(url);

		try {
			post.setEntity(new UrlEncodedFormEntity(list, "utf-8"));
			HttpResponse response = client.execute(post);

			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();

				SharedPreferences preferences = context.getSharedPreferences(
						"cookie", Context.MODE_PRIVATE);
				Editor editor = preferences.edit();

				editor.putString("cookie", response.getHeaders("Set-Cookie")[1]
						+ "");
				editor.commit();
				return EntityUtils.toString(entity);

			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return url;

	}

	public String postCookie(String url, List<BasicNameValuePair> list,
			Context context) {
		SharedPreferences preferences = context.getSharedPreferences("cookie",
				Context.MODE_PRIVATE);
		String cookie = preferences.getString("cookie", null);
		HttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost(url);

		post.setHeader("Cookie", cookie);
		HttpResponse response;
		try {
			post.setEntity(new UrlEncodedFormEntity(list, "utf-8"));
			response = client.execute(post);
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();
				return EntityUtils.toString(entity, "utf-8");
			}
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return url;
	}

	public static String sendData(String url, File file, String name,
			String region, String gender, String cookie) {
		try {

			HttpClient httpClient = new DefaultHttpClient();
			HttpContext localContext = new BasicHttpContext();
			httpClient.getParams().setParameter(
					CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

			HttpPost httpPost = new HttpPost(url);
			httpPost.setHeader("Cookie", cookie);
			MultipartEntity entity = new MultipartEntity(
					HttpMultipartMode.BROWSER_COMPATIBLE);
			FileBody filebody = new FileBody(file);
			entity.addPart("name", new StringBody(name));
			entity.addPart("region", new StringBody(region));
			entity.addPart("gender", new StringBody(gender));
			entity.addPart("avatar", filebody);
			httpPost.setEntity(entity);
			HttpResponse response = httpClient.execute(httpPost, localContext);
			HttpEntity entity2 = response.getEntity();
			return EntityUtils.toString(entity2, "utf-8");

		} catch (Exception e) {

			Log.v("myApp", "Some error came up");

		}
		return gender;

	}

	public static Bitmap downLoadBitmap(String url) {
		HttpClient client = new DefaultHttpClient();
		HttpGet get = new HttpGet(url);

		HttpResponse response;
		try {
			response = client.execute(get);
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();
				Bitmap bitmap = BitmapFactory.decodeStream(entity.getContent());
				return bitmap;
			}
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;

	}

	public static boolean downLaodDB(String url, String fileName,
			String packageName) {

		fileName = URLDecoder.decode(fileName);

		String directory = "/data/data/" + packageName + "/databases/";
		File file = new File(directory);
		if (!file.exists()) {
			file.mkdirs();
		}
		try {
			HttpClient client = new DefaultHttpClient();
			HttpParams params2 = client.getParams();
			HttpGet get = new HttpGet(url);
			HttpResponse response = client.execute(get);
			HttpConnectionParams.setConnectionTimeout(params2, 10 * 3000);
			HttpConnectionParams.setSoTimeout(params2, 10 * 5000);
			if (HttpStatus.SC_OK == response.getStatusLine().getStatusCode()) {
				HttpEntity entity = response.getEntity();
				InputStream input = entity.getContent();
				writeToSDCard(file, input);
				input.close();
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public static void writeToSDCard(File file, InputStream input) {

		try {

			FileOutputStream fos = new FileOutputStream(file);

			byte[] b = new byte[2048];
			int j = 0;

			while ((j = input.read(b)) != -1) {
				fos.write(b, 0, j);

			}
			fos.flush();
			fos.close();

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private static String read(HttpResponse response) throws Exception {
		String result = "";
		HttpEntity entity = response.getEntity();
		InputStream inputStream = null;
		ByteArrayOutputStream content = null;
		try {
			inputStream = entity.getContent();
			content = new ByteArrayOutputStream();

			Header header = response.getFirstHeader("Content-Encoding");
			if (header != null
					&& header.getValue().toLowerCase().indexOf("gzip") > -1) {
				inputStream = new GZIPInputStream(inputStream);
			}

			// Read response into a buffered stream
			int readBytes = 0;
			byte[] sBuffer = new byte[512];
			while ((readBytes = inputStream.read(sBuffer)) != -1) {
				content.write(sBuffer, 0, readBytes);
			}
			// Return result from buffered stream
			result = new String(content.toByteArray());
			return result;
		} catch (IllegalStateException e) {
			throw new Exception(e);
		} catch (IOException e) {
			throw new Exception(e);
		} finally {
			if (content != null) {
				content.flush();
				content.close();
			}
			if (inputStream != null) {
				inputStream.close();
			}
		}
	}

}
